//
//  Utility.m
//  AppFrameWork
//
//  Created by Le Anh Tai on 7/21/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "Utility.h"
#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>
#import "MBProgressHUD.h"
#import "Global.h"

@implementation Utility

+ (CGFloat)estimateTimeWithDis:(CGFloat) dis andVelocity:(CGFloat) vel
{
    CGFloat time = (dis / vel*1.5);
    return time > DEFAULT_SLOWEST_SPEED_TIME? DEFAULT_SLOWEST_SPEED_TIME:time;
}

+ (NSString*)stringCurrencyForInteger:(NSInteger)value
{
    NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
    NSNumber *number   = [NSNumber numberWithFloat:value];
    NSString *result;
    
    [fmt setFormatterBehavior: NSNumberFormatterBehaviorDefault];
    [fmt setCurrencySymbol: @""];
    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"vi"]];
    [fmt setNumberStyle: NSNumberFormatterDecimalStyle];
    
    result = [fmt stringFromNumber:number];
    return result;
}

+ (void) showLoading:(UIView*)vw withMsg:(NSString*)msg
{
    [MBProgressHUD hideAllHUDsForView:vw animated:NO];
    
    MBProgressHUD* hud = [MBProgressHUD showHUDAddedTo:vw animated:YES];
    hud.labelText = msg;
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeIndeterminate;
    
    vw.userInteractionEnabled = NO;
}

+ (void) hideLoading:(UIView*)vw
{
    vw.userInteractionEnabled = YES;
    
    [MBProgressHUD hideHUDForView:vw animated:YES];
}

+ (void) showInfoMsg:(NSString*)msg inView:(UIView*)vw
{
    [MBProgressHUD hideAllHUDsForView:vw animated:NO];
    
    MBProgressHUD* hud = [MBProgressHUD showHUDAddedTo:vw
                                              animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.labelText = @"";
    hud.detailsLabelText = msg;
    
    [hud performSelector:@selector(hide) withObject:nil afterDelay:1.5];
}

+ (NSString*)makeChangedString:(CGFloat)value preValue:(CGFloat)preValue
{
    NSString *ret = DEFAULT_CHANGED_LABEL;
    
    if (preValue == .0f)
    {
        return ret;
    }
    
    float diff = value - preValue;
    
    NSString *sign = @"+";
    
    if (diff < 0)
    {
        sign = @"";
    }
    
    ret = [NSString stringWithFormat:FORMAT_CHANGED_LABEL,
           sign, [self stringCurrencyForInteger: (int)diff],
           sign, diff*100/preValue];
    
    return ret;
}

+ (UILabel*)createLabelWithFont:(UIFont*)font
{
    UILabel *label = [[UILabel alloc]init];
    label.textColor = COLOR(246, 246, 246, 1);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = font;    
    label.backgroundColor = [UIColor clearColor];
    
    return label;
}

+ (id)getObject:(NSArray*)objects atIndex:(NSInteger)index
{
    if (objects == nil)
    {
        return nil;
    }
    
    if (index < 0 || [objects count] <= index)
    {
        return nil;
    }
    
    return [objects objectAtIndex:index];
}

+ (NSString*)getDateFromString:(NSString*)strDate inputFormat:(NSString*)inputFormat outputFormat:(NSString*)outputFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:inputFormat];
    NSDate *dateObj = [dateFormatter dateFromString:strDate];
    
    [dateFormatter setDateFormat:outputFormat];
    
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
    return [dateFormatter stringFromDate:dateObj];
}

+ (float)convertStringToFloat:(NSString *)value
{
    return [[value stringByReplacingOccurrencesOfString:@"." withString:@""] floatValue];
}

+(BOOL)isDate:(NSDate*)date1 ssGreaterOrEqualDate:(NSDate*)date2
{
    NSComparisonResult result = [date1 compareWithoutSecondWithDate:date2];
    
    if (result == NSOrderedSame || result ==NSOrderedDescending)
    {
        return YES;
    }
    
    return NO;
}
+ (CGSize)getSizeOfString:(NSString*)string font:(UIFont*)font width:(CGFloat)width
{
    if (font && string && string.length > 0)
    {
        CGSize expectedSize;
        
        if (IS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_IOS7)
        {
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
            NSDictionary *attributes = @{NSFontAttributeName : font};
            [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:string attributes:attributes]];
            
            CGSize size = CGSizeMake(width, CGFLOAT_MAX);
            CGRect rect = [attributedString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
            
            expectedSize = rect.size;
            expectedSize = CGSizeMake(ceilf(expectedSize.width), ceilf(expectedSize.height));
        }
        else
        {
            expectedSize = [string sizeWithFont:font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
        }
        
        return expectedSize;
    }
    
    return CGSizeZero;
}

+ (UIImage* )setBackgroundImageByColor:(UIColor *)backgroundColor withFrame:(CGRect )rect
{
    // tcv - temporary colored view
    UIView *tcv = [[UIView alloc] initWithFrame:rect];
    [tcv setBackgroundColor:backgroundColor];
    
    // set up a graphics context of button's size
    CGSize gcSize = tcv.frame.size;
    UIGraphicsBeginImageContext(gcSize);
    
    // add tcv's layer to context
    [tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    // create background image now
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
    
    //    [tcv release];
}

+(NSInteger)offsetTopForViewController
{
    if (IS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_IOS7)
    {
        return 20;
    }
    
    return 0;
}

@end
